// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.


suite("test_cast_to_bigint_from_decimal32_9_4") {

    // This test case is generated from the correspoinding be UT test case,
    // update this case if the correspoinding be UT test case is updated,
    // e.g.: ../run-be-ut.sh --run --filter=FunctionCastToDecimalTest.* --gen_regression_case
    sql "drop table if exists test_cast_to_bigint_from_decimal32_9_4_0_nullable;"
    sql "create table test_cast_to_bigint_from_decimal32_9_4_0_nullable(f1 int, f2 decimalv3(9, 4)) properties('replication_num'='1');"
    sql """insert into test_cast_to_bigint_from_decimal32_9_4_0_nullable values (0, "0.0000"),(1, "0.0000"),(2, "0.0001"),(3, "-0.0001"),(4, "0.0009"),(5, "-0.0009"),(6, "0.9999"),(7, "-0.9999"),(8, "0.9998"),(9, "-0.9998"),(10, "0.0999"),(11, "-0.0999"),(12, "0.9000"),(13, "-0.9000"),(14, "0.9001"),(15, "-0.9001"),(16, "1.0000"),(17, "-1.0000"),(18, "1.0001"),(19, "-1.0001"),
      (20, "1.0009"),(21, "-1.0009"),(22, "1.9999"),(23, "-1.9999"),(24, "1.9998"),(25, "-1.9998"),(26, "1.0999"),(27, "-1.0999"),(28, "1.9000"),(29, "-1.9000"),(30, "1.9001"),(31, "-1.9001"),(32, "9.0000"),(33, "-9.0000"),(34, "9.0001"),(35, "-9.0001"),(36, "9.0009"),(37, "-9.0009"),(38, "9.9999"),(39, "-9.9999"),
      (40, "9.9998"),(41, "-9.9998"),(42, "9.0999"),(43, "-9.0999"),(44, "9.9000"),(45, "-9.9000"),(46, "9.9001"),(47, "-9.9001"),(48, "99999.0000"),(49, "-99999.0000"),(50, "99999.0001"),(51, "-99999.0001"),(52, "99999.0009"),(53, "-99999.0009"),(54, "99999.9999"),(55, "-99999.9999"),(56, "99999.9998"),(57, "-99999.9998"),(58, "99999.0999"),(59, "-99999.0999"),
      (60, "99999.9000"),(61, "-99999.9000"),(62, "99999.9001"),(63, "-99999.9001"),(64, "99998.0000"),(65, "-99998.0000"),(66, "99998.0001"),(67, "-99998.0001"),(68, "99998.0009"),(69, "-99998.0009"),(70, "99998.9999"),(71, "-99998.9999"),(72, "99998.9998"),(73, "-99998.9998"),(74, "99998.0999"),(75, "-99998.0999"),(76, "99998.9000"),(77, "-99998.9000"),(78, "99998.9001"),(79, "-99998.9001"),
      (80, "9999.0000"),(81, "-9999.0000"),(82, "9999.0001"),(83, "-9999.0001"),(84, "9999.0009"),(85, "-9999.0009"),(86, "9999.9999"),(87, "-9999.9999"),(88, "9999.9998"),(89, "-9999.9998"),(90, "9999.0999"),(91, "-9999.0999"),(92, "9999.9000"),(93, "-9999.9000"),(94, "9999.9001"),(95, "-9999.9001"),(96, "90000.0000"),(97, "-90000.0000"),(98, "90000.0001"),(99, "-90000.0001"),
      (100, "90000.0009"),(101, "-90000.0009"),(102, "90000.9999"),(103, "-90000.9999"),(104, "90000.9998"),(105, "-90000.9998"),(106, "90000.0999"),(107, "-90000.0999"),(108, "90000.9000"),(109, "-90000.9000"),(110, "90000.9001"),(111, "-90000.9001"),(112, "90001.0000"),(113, "-90001.0000"),(114, "90001.0001"),(115, "-90001.0001"),(116, "90001.0009"),(117, "-90001.0009"),(118, "90001.9999"),(119, "-90001.9999"),
      (120, "90001.9998"),(121, "-90001.9998"),(122, "90001.0999"),(123, "-90001.0999"),(124, "90001.9000"),(125, "-90001.9000"),(126, "90001.9001"),(127, "-90001.9001")
      ,(128, null);
    """

    sql "set enable_strict_cast=true;"
    qt_sql_0_strict 'select f1, cast(f2 as bigint) from test_cast_to_bigint_from_decimal32_9_4_0_nullable order by 1;'

    sql "set enable_strict_cast=false;"
    qt_sql_0_non_strict 'select f1, cast(f2 as bigint) from test_cast_to_bigint_from_decimal32_9_4_0_nullable order by 1;'

    sql "drop table if exists test_cast_to_bigint_from_decimal32_9_4_0_not_nullable;"
    sql "create table test_cast_to_bigint_from_decimal32_9_4_0_not_nullable(f1 int, f2 decimalv3(9, 4)) properties('replication_num'='1');"
    sql """insert into test_cast_to_bigint_from_decimal32_9_4_0_not_nullable values (0, "0.0000"),(1, "0.0000"),(2, "0.0001"),(3, "-0.0001"),(4, "0.0009"),(5, "-0.0009"),(6, "0.9999"),(7, "-0.9999"),(8, "0.9998"),(9, "-0.9998"),(10, "0.0999"),(11, "-0.0999"),(12, "0.9000"),(13, "-0.9000"),(14, "0.9001"),(15, "-0.9001"),(16, "1.0000"),(17, "-1.0000"),(18, "1.0001"),(19, "-1.0001"),
      (20, "1.0009"),(21, "-1.0009"),(22, "1.9999"),(23, "-1.9999"),(24, "1.9998"),(25, "-1.9998"),(26, "1.0999"),(27, "-1.0999"),(28, "1.9000"),(29, "-1.9000"),(30, "1.9001"),(31, "-1.9001"),(32, "9.0000"),(33, "-9.0000"),(34, "9.0001"),(35, "-9.0001"),(36, "9.0009"),(37, "-9.0009"),(38, "9.9999"),(39, "-9.9999"),
      (40, "9.9998"),(41, "-9.9998"),(42, "9.0999"),(43, "-9.0999"),(44, "9.9000"),(45, "-9.9000"),(46, "9.9001"),(47, "-9.9001"),(48, "99999.0000"),(49, "-99999.0000"),(50, "99999.0001"),(51, "-99999.0001"),(52, "99999.0009"),(53, "-99999.0009"),(54, "99999.9999"),(55, "-99999.9999"),(56, "99999.9998"),(57, "-99999.9998"),(58, "99999.0999"),(59, "-99999.0999"),
      (60, "99999.9000"),(61, "-99999.9000"),(62, "99999.9001"),(63, "-99999.9001"),(64, "99998.0000"),(65, "-99998.0000"),(66, "99998.0001"),(67, "-99998.0001"),(68, "99998.0009"),(69, "-99998.0009"),(70, "99998.9999"),(71, "-99998.9999"),(72, "99998.9998"),(73, "-99998.9998"),(74, "99998.0999"),(75, "-99998.0999"),(76, "99998.9000"),(77, "-99998.9000"),(78, "99998.9001"),(79, "-99998.9001"),
      (80, "9999.0000"),(81, "-9999.0000"),(82, "9999.0001"),(83, "-9999.0001"),(84, "9999.0009"),(85, "-9999.0009"),(86, "9999.9999"),(87, "-9999.9999"),(88, "9999.9998"),(89, "-9999.9998"),(90, "9999.0999"),(91, "-9999.0999"),(92, "9999.9000"),(93, "-9999.9000"),(94, "9999.9001"),(95, "-9999.9001"),(96, "90000.0000"),(97, "-90000.0000"),(98, "90000.0001"),(99, "-90000.0001"),
      (100, "90000.0009"),(101, "-90000.0009"),(102, "90000.9999"),(103, "-90000.9999"),(104, "90000.9998"),(105, "-90000.9998"),(106, "90000.0999"),(107, "-90000.0999"),(108, "90000.9000"),(109, "-90000.9000"),(110, "90000.9001"),(111, "-90000.9001"),(112, "90001.0000"),(113, "-90001.0000"),(114, "90001.0001"),(115, "-90001.0001"),(116, "90001.0009"),(117, "-90001.0009"),(118, "90001.9999"),(119, "-90001.9999"),
      (120, "90001.9998"),(121, "-90001.9998"),(122, "90001.0999"),(123, "-90001.0999"),(124, "90001.9000"),(125, "-90001.9000"),(126, "90001.9001"),(127, "-90001.9001");
    """

    sql "set enable_strict_cast=true;"
    qt_sql_0_strict 'select f1, cast(f2 as bigint) from test_cast_to_bigint_from_decimal32_9_4_0_not_nullable order by 1;'

    sql "set enable_strict_cast=false;"
    qt_sql_0_non_strict 'select f1, cast(f2 as bigint) from test_cast_to_bigint_from_decimal32_9_4_0_not_nullable order by 1;'

}